#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#include "../src/index/bw_tree.h"
#include "../src/memory/memory_manager.h"

int main() {
    printf("Simple Bw-Tree Test\n");
    printf("==================\n");
    
    // Test 1: Basic initialization
    printf("1. Testing memory manager initialization...\n");
    memory_manager_t* mm = mm_init(1024 * 1024);
    if (!mm) {
        printf("❌ Memory manager initialization failed\n");
        return 1;
    }
    printf("✓ Memory manager initialized\n");
    
    // Test 2: Tree creation
    printf("2. Testing tree creation...\n");
    bw_tree_t* tree = bw_tree_create(mm);
    if (!tree) {
        printf("❌ Tree creation failed\n");
        mm_cleanup(mm);
        return 1;
    }
    printf("✓ Tree created successfully\n");
    printf("   Root page ID: %llu\n", (unsigned long long)tree->root_page_id);
    printf("   Total nodes: %llu\n", (unsigned long long)atomic_load(&tree->total_nodes));
    
    // Test 3: Simple insert
    printf("3. Testing simple insert...\n");
    const char* key = "test_key";
    const char* value = "test_value";
    
    bw_result_t result = bw_tree_insert(tree, key, strlen(key), value, strlen(value));
    printf("   Insert result: %d\n", result);
    
    if (result != BW_SUCCESS) {
        printf("❌ Insert failed with code: %d\n", result);
        
        // Print some debugging info
        printf("   Tree state:\n");
        printf("     - Root page ID: %llu\n", (unsigned long long)tree->root_page_id);
        printf("     - Is destroyed: %s\n", atomic_load(&tree->is_destroyed) ? "true" : "false");
        printf("     - Total nodes: %llu\n", (unsigned long long)atomic_load(&tree->total_nodes));
        printf("     - Active operations: %u\n", atomic_load(&tree->active_operations));
        
        bw_tree_destroy(tree);
        mm_cleanup(mm);
        return 1;
    }
    printf("✓ Insert successful\n");
    
    // Test 4: Simple search
    printf("4. Testing simple search...\n");
    bw_search_result_t search_result;
    result = bw_tree_search(tree, key, strlen(key), &search_result);
    printf("   Search result: %d\n", result);
    
    if (result == BW_SUCCESS && search_result.found) {
        printf("✓ Search successful\n");
        printf("   Found value size: %u\n", search_result.value->size);
        printf("   Expected size: %zu\n", strlen(value));
        
        if (search_result.value->size == strlen(value) && 
            memcmp(search_result.value->data, value, strlen(value)) == 0) {
            printf("✓ Value matches\n");
        } else {
            printf("❌ Value mismatch\n");
        }
        
        // Clean up search result
        if (search_result.value) {
            bw_value_destroy(search_result.value, mm);
        }
    } else {
        printf("❌ Search failed or key not found\n");
    }
    
    // Test 5: Tree cleanup
    printf("5. Testing tree cleanup...\n");
    bw_tree_destroy(tree);
    printf("✓ Tree destroyed\n");
    
    mm_cleanup(mm);
    printf("✓ Memory manager cleaned up\n");
    
    printf("\nSimple test completed successfully! ✅\n");
    return 0;
}